﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Data;

public partial class admin_Articles_ArticleHot : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["url_back_admin"] = Request.RawUrl;
            ShowData();
            LoadCategory();
        }
    }
    ArticleHotBLL data = new ArticleHotBLL();
    CategoryBLL categoryData = new CategoryBLL();
    ArticleBLL articleData = new ArticleBLL();

    private void LoadCategory()
    {
        dropCategory.Items.Clear();
        ShowChild(0, " - - ");
    }
    private void LoadArticle()
    {
        if (dropCategory.Items.Count > 0)
        {
            int id = Convert.ToInt32(dropCategory.SelectedValue);
            List<Article> list = articleData.GetByCategory(id);
            DataTable table = new DataTable();
            table.Columns.Add("Id");
            table.Columns.Add("Name");
            foreach (var item in list)
            {
                DataRow row = table.NewRow();
                row["Id"] = item.Id.ToString();
                row["Name"] = item.Title;
                table.Rows.Add(row);
            }
            dropArticle.DataSource = table;
            dropArticle.DataValueField = "Id";
            dropArticle.DataTextField = "Name";
            dropArticle.DataBind();
        }
    }
    private void ShowChild(int parentId, string space)
    {
        List<Category> listDataSource = categoryData.GetByParent(parentId);

        foreach (Category item in listDataSource)
        {
            ListItem obj = new ListItem(space + item.Name, item.Id.ToString());
            dropCategory.Items.Add(obj);

            ShowChild(item.Id, space + "- - - ");
        }
    }
    private void ShowData()
    {
        List<ArticleHot> list = new List<ArticleHot>();
        int id = -1;
        try { id = Convert.ToInt32(dropCategory.SelectedValue); }
        catch { }
        list = data.GetByCateId(id);
        if (dropIsEnable.SelectedIndex != 0)
            list = list.Where(p => p.isEnable == Convert.ToBoolean(dropIsEnable.SelectedValue)).ToList();

        gridView.DataSource = GetProduct(list);
        gridView.DataBind();

        //Bo mui ten len xuong dau va cuoi
        if (gridView.Rows.Count > 0)
        {
            gridView.Rows[0].Cells[6].Enabled = false;
            gridView.Rows[gridView.Rows.Count - 1].Cells[5].Enabled = false;
        }
    }
    private List<Article> GetProduct(List<ArticleHot> list)
    {
        List<Article> listProduct = new List<Article>();
        foreach (var item in list)
        {
            Article article = articleData.GetById(item.Id);
            if (article != null)
                listProduct.Add(article);
        }

        return listProduct;
    }
    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowIndex >= 0) //Bo qua dong dau
            {
                int id = Convert.ToInt32(e.Row.Cells[1].Text);
                var article = articleData.GetById(Convert.ToInt32(e.Row.Cells[1].Text));
                if (e.Row.Cells[3].Text == "0") e.Row.Cells[3].Text = "Danh mục gốc";
                else e.Row.Cells[3].Text = categoryData.GetById(Convert.ToInt32(e.Row.Cells[3].Text)).Name;
                bool isEnable = data.GetById(id).isEnable;

                //Icon check
                Image img = new Image();
                foreach (Control item in e.Row.Cells[7].Controls)
                {
                    if (item.GetType() == typeof(Image))
                    {
                        img = (Image)item;
                        break;
                    }
                }
                if (isEnable)
                    img.ImageUrl = "~/Images/check.png";
                else
                {
                    img.ImageUrl = "~/Images/an.png";

                }
            }
        }
        catch { }
    }
    protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        ShowData();
    }
    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        lbThongBao.Text = "";
        try
        {
            gridView.PageIndex = e.NewPageIndex;
            ShowData();
        }
        catch { }
    }
    protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[1].Text);
            data.Delete(id);
            lbThongBao.Text = "Bài viết đã được xóa";

            ShowData();
        }
        catch (Exception ex)
        {
            lbThongBao.Text = ex.Message;
        }
    }
    private void Enable(bool isEnable)
    {
        foreach (GridViewRow item in gridView.Rows)
        {
            foreach (Control ctrl in item.Cells[0].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    if (check.Checked)
                    {
                        int id = Convert.ToInt32(item.Cells[1].Text);
                        data.Enable(id, isEnable);
                    }
                    break;
                }
            }
        }
        ShowData();
    }
    protected void btnVisible_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        lbThongBao.Text = "";
        Enable(false);
    }
    protected void btnEnable_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        lbThongBao.Text = "";
        Enable(true);
    }
    protected void btnXoaChon_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            foreach (GridViewRow item in gridView.Rows)
            {
                foreach (Control ctrl in item.Cells[0].Controls)
                {
                    if (ctrl.GetType() == typeof(CheckBox))
                    {
                        CheckBox check = (CheckBox)ctrl;
                        if (check.Checked)
                        {
                            int id = Convert.ToInt32(item.Cells[1].Text);
                            data.Delete(id);
                        }
                        break;
                    }
                }
            }
            ShowData();
            lbThongBao.Text = "Bài viết đã được xóa";
        }
        catch (Exception ex)
        {
            lbThongBao.Text = ex.Message;
        }
    }
    protected void linkCheckAll_Click(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        foreach (GridViewRow item in gridView.Rows)
        {
            foreach (Control ctrl in item.Cells[0].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    check.Checked = true;
                    break;
                }
            }
        }
    }
    protected void linkUnCheckAll_Click(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        foreach (GridViewRow item in gridView.Rows)
        {
            foreach (Control ctrl in item.Cells[0].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    check.Checked = false;
                    break;
                }
            }
        }
    }
    protected void dropCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        LoadArticle();
        ShowData();
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            if (dropArticle.Items.Count > 0)
            {
                int id = Convert.ToInt32(dropArticle.SelectedValue);
                int cateId = Convert.ToInt32(dropCategory.SelectedValue);
                if (data.CheckExist(id,cateId))
                {
                    lbThongBao.Text = "Bài viết này đã có trong danh sách";
                    return;
                }
                if (data.GetCountItem(Convert.ToInt32(dropCategory.SelectedValue)) >= 6)
                {
                    lbThongBao.Text = "Tối đa là 6 bài viết trong 1 danh mục";
                    return;
                }
                ArticleHot articleHot = new ArticleHot();
                articleHot.Sort = data.GetMaxSort();
                articleHot.Id = id;
                articleHot.isEnable = true;
                articleHot.Category = Convert.ToInt32(dropCategory.SelectedValue);
                data.Add(articleHot);
                lbThongBao.Text = "Bài viết đã được thêm";
                ShowData();
            }
        }
        catch (Exception ex)
        {
            lbThongBao.Text = ex.Message;
        }
    }
    protected void gridView_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            if (e.CommandName.Equals("Up"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                int idFirst = Convert.ToInt32(gridView.Rows[index].Cells[1].Text);
                int idSecond = Convert.ToInt32(gridView.Rows[index - 1].Cells[1].Text);

                data.SwapSort(idFirst, idSecond);
            }
            else if (e.CommandName.Equals("Down"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                int idFirst = Convert.ToInt32(gridView.Rows[index].Cells[1].Text);
                int idSecond = Convert.ToInt32(gridView.Rows[index + 1].Cells[1].Text);

                data.SwapSort(idFirst, idSecond);
            }
            ShowData();

        }
        catch (Exception ex)
        {
            Myfunction.ThongBao(lbThongBao, ex.Message);
        }
    }
    protected void dropPosition_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        ShowData();
    }
    protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
        ShowData();
    }
}